package com.example.libraryms.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.libraryms.exception.MyException;
import com.example.libraryms.mapper.BookMapper;
import com.example.libraryms.model.dto.*;
import com.example.libraryms.model.entity.Book;
import com.example.libraryms.service.BookService;
import com.example.libraryms.service.BookTypeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author Cauli
 * @date 2023/7/31 11:56
 * @description 图书 服务类
 */
@Service
public class BookServiceImpl extends ServiceImpl<BookMapper, Book> implements BookService {
    @Autowired
    private BookTypeService bookTypeService;

    @Override
    public Page<Book> getBookPage(BookQueryDTO bookQueryDTO) {
        LambdaQueryWrapper<Book> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(StrUtil.isNotEmpty(bookQueryDTO.getName()), Book::getName, bookQueryDTO.getName())
                .like(StrUtil.isNotEmpty(bookQueryDTO.getAuthor()), Book::getAuthor, bookQueryDTO.getAuthor())
                .like(StrUtil.isNotEmpty(bookQueryDTO.getPubCompany()), Book::getPubCompany, bookQueryDTO.getPubCompany())
                .gt(bookQueryDTO.getMinPrice() != null, Book::getPrice, bookQueryDTO.getMinPrice())
                .lt(bookQueryDTO.getMaxPrice() != null, Book::getPrice, bookQueryDTO.getMaxPrice())
                .eq(Book::getDeleted, 0);
        return baseMapper.selectPage(bookQueryDTO.toPage(), lambdaQueryWrapper);
    }

    @Override
    public BookDetailDTO getBookDetail(Integer bookNo) {
        BookDetailDTO bookDetailDTO = new BookDetailDTO();
        // 根据图书编号获取图书信息
        Book book = baseMapper.selectById(bookNo);
        if (book == null) {
            throw new MyException("图书不存在");
        }
        BeanUtils.copyProperties(book, bookDetailDTO);
        // 根据图书分类编号获取图书分类信息
        BookTypeDTO bookTypeDTO = bookTypeService.getBookType(book.getTypeNo());
        if (bookTypeDTO != null) {
            BeanUtils.copyProperties(bookTypeDTO, bookDetailDTO);
        }
        return bookDetailDTO;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addBook(BookAddDTO bookAddDTO) {
        Book book = new Book();
        if (bookAddDTO.getTypeNo() == null) {
            throw new MyException("不能增加无分类的图书");
        }
        // 根据图书分类编号获取图书分类信息
        bookTypeService.getBookType(bookAddDTO.getTypeNo());
        BeanUtil.copyProperties(bookAddDTO, book);
        baseMapper.insert(book);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateBook(BookUpdateDTO bookUpdateDTO) {
        // 根据图书编号获取图书信息
        Book book = baseMapper.selectById(bookUpdateDTO.getNo());
        if (book == null) {
            throw new MyException("图书不存在");
        }
        BeanUtil.copyProperties(bookUpdateDTO, book);
        baseMapper.updateById(book);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBooks(List<Integer> bookNoList) {
        for (Integer no : bookNoList) {
            // 根据图书编号获取图书信息
            Book book = baseMapper.selectById(no);
            if (book == null) {
                throw new MyException("图书不存在");
            }
            baseMapper.deleteById(book);
        }
    }
}
